package com.lv.common.utils;

import android.os.CountDownTimer;

import java.text.ParseException;

/**
 * 作者： ygx
 * 创建日期：2022/12/6 11:49 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
public class CountDownUtil {

    private CountDownTimer mTimer;

    private int interval = 1000;

    public CountDownUtil() {
    }

    /**
     * 日期转时间戳
     *
     * @param date
     * @param format
     * @return
     */
    public static String getTimeStamp(String date, String format) {
//        return DateUtil.strToDate(date, format).getTime();
        try {
            return TimeUtil.dateToStampByFormat(date, format);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 开始倒计时
     *
     * @param startTime      开始时间（时间戳）
     * @param minuteInterval 时间间隔（单位：分）
     * @param callBack
     */
    public void start(long startTime, int minuteInterval, OnCountDownCallBack callBack) {
        long lengthTime = minuteInterval * 60 * interval;
        //查看是否为毫秒的时间戳
        boolean isMillSecond = (String.valueOf(startTime).length() == 13);
        startTime = startTime * (isMillSecond ? 1 : interval);
        long endTime = startTime + lengthTime;
        long curTime = System.currentTimeMillis();
        mTimer = getTimer(endTime - curTime, interval, callBack);
        if (Math.abs(curTime - startTime) > lengthTime) {
            if (callBack != null) {
                callBack.onFinish();
            }
        } else {
            mTimer.start();
        }
    }

    /**
     * 开始倒计时
     *
     * @param startTime      开始时间（时间戳）
     * @param secondInterval 时间间隔（单位：type 0 分 1 秒 ）
     * @param type           时间间隔单位 0 分 1 秒
     * @param callBack
     */
    public void start(long startTime, int secondInterval, COUTDOWN type, OnCountDownCallBack callBack) {
        long lengthTime = 0L;
        if (type == COUTDOWN.TIME_M) {
            lengthTime = secondInterval * 60 * interval;

        } else {
            lengthTime = secondInterval * interval;
        }
        //查看是否为毫秒的时间戳
        boolean isMillSecond = (String.valueOf(startTime).length() == 13);
        startTime = startTime * (isMillSecond ? 1 : interval);
        long endTime = startTime + lengthTime;
        long curTime = System.currentTimeMillis();
        mTimer = getTimer(endTime - curTime, interval, callBack);
        if (Math.abs(curTime - startTime) > lengthTime) {
            if (callBack != null) {
                callBack.onFinish();
            }
        } else {
            mTimer.start();
        }
    }

    /**
     * 开始倒计时
     *
     * @param startTime      开始时间（时间戳）
     * @param secondInterval 时间间隔（单位：type 0 分 1 秒 ）
     * @param type           时间间隔单位 0 分 1 秒
     * @param callBack
     */
    public void start(long startTime, long secondInterval, COUTDOWN type, OnCountDownCallBack callBack) {
        long lengthTime = 0L;
        if (type == COUTDOWN.TIME_M) {
            lengthTime = secondInterval * 60 * interval;

        } else {
            lengthTime = secondInterval * interval;
        }
        //查看是否为毫秒的时间戳
        boolean isMillSecond = (String.valueOf(startTime).length() == 13);
        startTime = startTime * (isMillSecond ? 1 : interval);
        long endTime = startTime + lengthTime;
        long curTime = System.currentTimeMillis();
        mTimer = getTimer(endTime - curTime, interval, callBack);
        if (Math.abs(curTime - startTime) > lengthTime) {
            if (callBack != null) {
                callBack.onFinish();
            }
        } else {
            mTimer.start();
        }
    }

    private CountDownTimer getTimer(long millisInFuture, long interval, OnCountDownCallBack callBack) {
        return new CountDownTimer(millisInFuture, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                int day = 0;
                int hour = 0;
                int minute = (int) (millisUntilFinished / interval / 60);
                int second = (int) (millisUntilFinished / interval % 60);
                if (minute > 60) {
                    hour = minute / 60;
                    minute = minute % 60;
                }
                if (hour > 24) {
                    day = hour / 24;
                    hour = hour % 24;
                }
                if (callBack != null) {
                    callBack.onProcess(day, hour, minute, second);
                }
            }

            @Override
            public void onFinish() {
                if (callBack != null) {
                    callBack.onFinish();
                }
            }
        };
    }

    /**
     * 开始倒计时
     *
     * @param endTime  结束时间（时间戳）
     * @param callBack
     */
    public void start(long endTime, OnCountDownCallBack callBack) {
        long curTime = System.currentTimeMillis();
        mTimer = getTimer(endTime - curTime, interval, callBack);
        if (endTime < curTime) {
            if (callBack != null) {
                callBack.onFinish();
            }
        } else {
            mTimer.start();
        }
    }

    /**
     * 必用
     */
    public void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    public interface OnCountDownCallBack {

        void onProcess(int day, int hour, int minute, int second);

        void onFinish();
    }
   public enum  COUTDOWN {
        //分
        TIME_M,
        //秒
        TIME_S
    }
}





